﻿class PlayerMenu {
	private	static var _xLocation:Number = 5;
	private	static var _yLocation:Number = 310;
	private	static var _depth:Number = 3000;
	private static var _menuHeight = 47;
	private static var _attack = true;
	private static var _move = true;
	private static var _item = true;
	private static var _switch = true;
	private static var _run = true;
	private static var _pass = true;
	
	public static function createPlayerMenu(p_depth:Number, p_player:Player, p_demon:Demon, p_attack:Boolean, p_move:Boolean, p_side:String) {
		_depth = p_depth;
		_attack = p_attack;
		_move = p_move;
		
		if (p_side == "left") {
			_xLocation = 5;
		} else {
			_xLocation = 595;
		}
		
		var _menuItems:Array = new Array("Attack", "Move", "Item", "Switch", "Run", "Pass");
		var _acceptable:Array = new Array(_attack, _move, _item, _switch, _run, _pass);
		
		Graphics.loadAndDrawObject("menuTop", p_depth - 1, "menus/menuTop.png", _xLocation, _yLocation - 23);
		
		for (var i=0; i < _menuItems.length; i++) {
			var _depth:Number = p_depth + i;
			Graphics.menuButton(_menuItems[i], _depth, _xLocation, _yLocation + (i*_menuHeight), _acceptable[i]); //, p_isValid);
		}
		//Graphics.menuButton("MOVE", p_depth + 1, _xLocation, _yLocation + 47);
		//Graphics.menuButton("ITEM", p_depth + 2, _xLocation, _yLocation + 94);
		//Graphics.menuButton("SWITCH", p_depth + 3, _xLocation, _yLocation + 141);
		//Graphics.menuButton("RUN", p_depth + 4, _xLocation, _yLocation + 188);
		//Graphics.menuButton("PASS", p_depth + 5, _xLocation, _yLocation + 235);
		
		
		_root.Attack.onRelease = function () {
			if (_attack) { menuButtonAttack(p_player, p_demon); };
		}
		_root.Move.onRelease = function () {
			if (_move) {menuButtonMove(); };
		}
		_root.Item.onRelease = function () { menuButtonItem(); };
		_root.Switch.onRelease = function () { menuButtonSwitch(); };
		_root.Run.onRelease = function () { menuButtonRun(); };
		_root.Pass.onRelease = function () { menuButtonPass(); };
	}
	
	public static function removePlayerMenu() {
		removeMovieClip("Attack");
		removeMovieClip("Move");
		removeMovieClip("Item");
		removeMovieClip("Switch");
		removeMovieClip("Run");
		removeMovieClip("Pass");
		removeMovieClip("Back");
		/*for (var i = 0; i <= 5; i++) {
			var p_y:Number =  _yLocation + (i*_menuHeight);
			removeMovieClip("Menu" + p_y);
		}*/
	}
	
	public static function menuButtonAttack(p_player:Player, p_demon:Demon) {
		_root._battCont.removeEverything();
		removePlayerMenu();
		var _attacks = p_demon.getAttacks();
		var _terrainAttacks = p_demon.getTerrainAttacks();
		
		Graphics.menuButton("Back", _depth, _xLocation, _yLocation, true);
		_root.Back.onRelease = function () { menuButtonBack(_depth, p_player, p_demon); };
		
		for (var i = 0; i < _attacks.length; i++) {
			var _next:Number = i + 1;
			var _nextName:String = _attacks[i].getName();
			var _acceptable:Array = new Array(_attack, _move, _item, _switch, _run, _pass);
			
			Graphics.menuButton(_nextName, _depth + _next, _xLocation, _yLocation + (_next*47), true);
			
			_root[_nextName]._nextAttack = _attacks[i];			// this attack must be added to the object so it has access to it
			_root[_nextName]._demon = p_demon;
			_root[_nextName].onRelease = function () {
				_root._battCont.attackSelection(this._demon, this._nextAttack);
			}
		}
		
		for (var i = 0; i < _terrainAttacks.length; i++) {
			var _newNext:Number = _attacks.length + i + 1;
			var _nextName:String = _terrainAttacks[i].getName();
			
			Graphics.menuButton(_nextName, _depth + _newNext, _xLocation, _yLocation + (_newNext*47), true);
			
			_root[_nextName]._nextAttack = _terrainAttacks[i];			// this attack must be added to the object so it has access to it
			_root[_nextName]._demon = p_demon;
			_root[_nextName].onRelease = function () {
				_root._battCont.terrainAttackSelection(this._demon, this._nextAttack);
			}
		}
	}
	
	public static function menuButtonMove() {
		_root._battCont.moveSelection();
	}
	
	public static function menuButtonItem() {
		trace("menu: item");
	}
	
	public static function menuButtonSwitch() {
		trace("menu: switch");
	}
	
	public static function menuButtonRun() {
		trace("menu: run");
	}
	
	public static function menuButtonPass() {
		_root._battCont.passSelection();
	}
	
	public static function menuButtonBack(p_depth:Number, p_player:Player, p_demon:Demon) {
		_root._battCont.removeEverything();
		removePlayerMenu();
		_root._battCont._currentState._state = _root._battCont._currentState._currentPlayer;
		if (_root._battCont._currentState._currentPlayer == "ai") {
			createPlayerMenu(p_depth, p_player, p_demon, _attack, _move, "right");
		} else {
			createPlayerMenu(p_depth, p_player, p_demon, _attack, _move, "left");
		}
	}
}